#ifndef VCODE_HPP
#define VCODE_HPP

#include <opencv2/opencv.hpp>
#include <qqrencode.h>
#include <QImage>

class VCode
{
public:
    static bool GetIP(std::vector<std::string> &ipList)
    {
        ipList.clear();

        const char* shellstr="ifconfig | grep 'Bcast'| tr -s ' ' : | cut -d: -f4";

        FILE *fp = popen(shellstr, "r");

        if(fp)
        {
            while(true)
            {
                char line[20];
                memset(line, 0, sizeof(line));


                if(fgets(line, 20, fp)!=NULL)
                {
                    //    puts(line);
                    ipList.push_back(std::string(line));
                    ipList.back().pop_back();
                }
                else
                    break;
            }

            pclose(fp);
            return true;
        }
        return false;
    }


    static cv::Mat GenCode(std::string message)
    {
        QQREncode encoder;
        encoder.encode(QString::fromStdString(message));
        QImage qrcode = encoder.toQImage();
        qrcode.scaled(150,150).save("qrcode.bmp","BMP");
        cv::Mat m=cv::imread("qrcode.bmp");
        remove("qrcode.bmp");
        return m;
    }


    static int ShowCode()
    {
        std::vector<std::string> ipl;
        if(!GetIP(ipl))
        {
            std::cout<<"fail to get ip\n";
            return 0;
        }

        for(auto &ip:ipl)
        {
//            std::cout<<"code ip="<<ip<<"\n";
            cv::Mat m=GenCode(ip+" 7654 00");
            cv::Mat m2=GenCode(ip+" 7654 01");
            cv::Mat canvas;
            cv::hconcat(m,m2,canvas);
            cv::imshow(ip,canvas);
        }
        cv::waitKey(1000);

        return ipl.size();
    }
};

#endif // VCODE_HPP

